import { ss } from '@/utils/storage'

const LOCAL_NAME = 'userStorage'

export interface UserInfo {
  avatar: string
  name: string
  description: string
  balance: string
  subscribeTime: string | null
  token: string | null
  resourceMap: Object
}

export interface UserState {
  userInfo: UserInfo
}

export function defaultSetting(): UserState {
  return {
    userInfo: {
      avatar: 'https://raw.githubusercontent.com/Chanzhaoyu/chatgpt-web/main/src/assets/avatar.jpg',
      name: '未登录',
      description: '',
      balance: '0.00',
      subscribeTime: null,
      token: null,
      resourceMap: {
        image: 0,
        edit: 0,
        moderation: 0,
        gpt4: 0,
        audio: 0,
        embedding: 0,
        fineTunes: 0,
      },
    },
  }
}

export function getLocalState(): UserState {
  const localSetting: UserState | undefined = ss.get(LOCAL_NAME)
  return { ...defaultSetting(), ...localSetting }
}

export function setLocalState(setting: UserState): void {
  ss.set(LOCAL_NAME, setting)
}
